<?php 	
	include_once("connect.php");
	include_once("comment_model.php");
	include_once("post_model.php");
	include_once("tag_model.php");
	include_once("utility.php");
	session_start();
	mysql_select_db("progin_171_13509047",$con);		
?>

<?php include_once "header.php" ?>

<?php 		
	//inisialisasi variabel2
	$comment_model = new Comment_Model;	
	$post_model = new Post_Model;
	$tag_model = new Tag_Model;
	
	
	$pageLoadTime = new DateTime(null, new DateTimeZone('Asia/Jakarta'));		
	$pageLoadTimeString = $pageLoadTime->format("Y-m-d\TH:i:s");
	
	$logged_in_user_id = -1;
	if(isset($_SESSION["userid"])) {
		$logged_in_user_id = $_SESSION["userid"];				
	}	
	
	if(!isset($_GET["id"])) {
		//tampilkan pesan error
		die("invalid link");
	}	
	$post_id = $_GET["id"];
	$post_info = mysql_fetch_array($post_model->getPost($post_id));
	$poster_name = $post_info["username"];	
	$poster_id = $post_info["poster_id"];
	
	$post_datetime = new DateTime($post_info['datetime'], new DateTimeZone('Asia/Jakarta'));	
	$now = new DateTime(null, new DateTimeZone('Asia/Jakarta'));	
	$post_diff_datetime_string = Utility::getDeltaTimeString($post_datetime, $now);		
		
	$post_title = $post_info["title"];
	$post_comment_count = $post_info["post_comment_count"];
	$post_like_count = $post_info["post_like_count"];
	$post_content = "";	
	if($post_info['post_type']=='link') {
		$temp = explode("\n", $post_info['content']);
		$link = $temp[0];
		$description = $temp[1];		
		$post_content = "<h2>".$link."</h2>"." "."deskripsi : ".$description;
	}else if($post_info['post_type']=="image") {
		$post_content = "<img class='fullimage' src='upload/".$post_info['content']."' />";
	}else if($post_info['post_type']=="video") {
		$tube_id = substr($post_info['content'],31, strlen($post_info['content']) + 1);		
		$post_content = '<div id="video">'.
						'<object type="application/x-shockwave-flash" style="width:560px; height:315px;" data="http://www.youtube.com/v/'.$tube_id.'?version=3">'.
						'<param name="movie" value="http://www.youtube.com/v/'.$tube_id.'?version=3" />'.
						'<param name="allowFullScreen" value="true" />'.
						'<param name="allowscriptaccess" value="always" />'.
						'</object>'.
						'</div>';
	}				
	
?>

	<!-- Body -->
	<div id="bodycontent">
	  <div class="contentcontainer">
	    
		<div class="title-text">
		  <h1><?php echo $post_title; ?></h1>
		</div>
		
		<div class="contentbox">		  
		  <div id="content-desc">
			<p><?php echo $post_content ?></p>
			<p><small><i>Uploaded by <a href="profile.php?id=<?php echo $poster_id; ?>"><?php echo $poster_name; ?></a> <?php echo $post_diff_datetime_string; ?> </i></small></p>
			<p><small>
				<?php 
					echo "tags : ";
					$post_tags_res = $tag_model->getTags($post_id);				
					$aTag = mysql_fetch_array($post_tags_res);
					echo $aTag["type"];				
					while($aTag = mysql_fetch_array($post_tags_res)) {
						echo ", ".$aTag["type"];
					}			 			
				?>
				</small>
			</p>
			<p><small>
				<?php 
					echo "<span id='comment-count'>".$post_comment_count."</span> peoples commented";
				?>
			</small></p>
		  </div>		  
		  <div id="content-like">
				<?php
				
					echo "<a onclick=doLikeOp(\"like\@".$_GET["id"]."\",\"id\")><img src=like.png width=20 height=20 alt=like></a>";
					echo "<a onclick=doLikeOp(\"dislike\@".$_GET["id"]."\",\"id\")><img src=dislike.png width=20 height=20 alt=dislike></a>";
					$post_model->printLike($post_like_count,$logged_in_user_id,$_GET["id"]);
				?>
	      </div>			  
			<?php if(isset($_SESSION["userid"])) : ?>
			  <h4>Tambahkan Komentar</h4>			  
			  <div id="content-inp-comment">
				Komentar
				<br/>				
				<textarea name="content" id="content-input-comment" rows="2" cols="20"></textarea>
				<span id="comment-content-error" class='form-error'></span>
			  </div>
			  <div id="content-add-comment">
				<input type="submit" value="Tambahkan komentar" id="content-post-comment" onclick="addComment(document.getElementById('content-input-comment').value, <?php echo $post_id ?>, <?php echo $logged_in_user_id ?>);" />
			  </div>
			  <input type="hidden" name="submitted" value="1"> 		
			  <?php endif; ?>
		  <div id="content-comments">
			<?php	
				$comment_res = $comment_model->getAllComment($post_id);	
				if($comment_res!=false) {
					while($row = mysql_fetch_array($comment_res)) {
					
						$comment_datetime = new DateTime($row['datetime'], new DateTimeZone('Asia/Jakarta'));	
						$now = new DateTime(null, new DateTimeZone('Asia/Jakarta'));	
						$comment_diff_datetime_string = Utility::getDeltaTimeString($comment_datetime, $now);
					
						echo '<div class="content-comment" id="comment'.$row['comment_id'].'">' ;
						echo '<img class="avatar" src="avatar/'.$row['avatar'].'" />';
						echo '<h4>'.$row['username'].'</h4>' ;
						echo '<p>'.$row['content'].'</p>' ;						
						echo '<span name="comment-date"><small>'.$comment_diff_datetime_string.'</small></span>' ;						
						if($row['username_id']==$logged_in_user_id)
							echo '<input type="button" onclick="delComment('.$row['comment_id'].');" value="delete" />';
						echo '</div>' ;
					}
				}				
			?>						 							
		  </div>
		</div>

	  </div>
	</div>	
	
	<input type="hidden" id="pageLoadTime" value="" />
	
	<script type="text/javascript">
		document.getElementById("pageLoadTime").value = new Date();		
	</script>
	
  </body>
</html>

<?php
	include_once("close_con.php");
?>